﻿$(function () {
    var totalPage = $("#mypromotioncodeTotlaPage").html();
    var totalCount = $("#mypromotioncodeTotlaCount").html();
    var globalStatus = 0;
    /*---------------------显示对应类型的的优惠券--------------------*/
    //得到未使用的优惠券
    $("#noUse").click(function () {
        totalPage = $("#noUsePromotionCodeTotalPage").val();
        totalCount = $("#noUsePromotionCodeTotalCount").val();
        getPromotionCode(0);
    });

    //得到已使用的优惠券
    $("#used").click(function () {
        totalPage = $("#usedPromotionCodeTotalPage").val();
        totalCount = $("#usedPromotionCodeTotalCount").val();
        getPromotionCode(1);
    });

    //得到已过期的优惠券
    $("#expire").click(function () {
        totalPage = $("#expirePromotionCodeTotalPage").val();
        totalCount = $("#expirePromotionCodeTotalCount").val();
        getPromotionCode(2);
    });

    /*---------------------得到优惠券--------------------*/
    function getPromotionCode(status) {
        $.ajax({
            type: "GET",
            url: "MyPromotionCodeByAjaxRequest?status=" + status + "&pageIndex=0",
            success: function (response) {
                if (status == 0) {
                    $("#mypromotioncodelistTab1").empty();
                    $("#mypromotioncodelistTab1").append(response);
                }
                else if (status == 1) {
                    $("#mypromotioncodelistTab2").empty();
                    $("#mypromotioncodelistTab2").append(response);
                }
                else if (status == 2) {
                    $("#mypromotioncodelistTab3").empty();
                    $("#mypromotioncodelistTab3").append(response);
                }
                globalStatus = status;
                contrlPageNumberDisplay();
            },
            error: function (err) {
                alert(err.responseText);
            }
        });
    }

    var $fristPage = $("#fristPage");
    var $previousPage = $("#previousPage");
    var $nextPage = $("#nextPage");
    var $lastPage = $("#lastPage");

    var currentPage = 0;
    contrlPageNumberDisplay();
    /*-------------分页---------------*/
    //控制页码是否显示
    function contrlPageNumberDisplay() {
        if (totalPage == 0) {
            $(".pageNavBottom").hide();
            return;
        }
        else {
            $(".pageNavBottom").show();
        }
        $('#currentPage').html(currentPage + 1);
        //当处于首页的时候,'上一页'隐藏
        if (currentPage == 0) {
            $previousPage.hide();
        }
        else {
            $previousPage.show();
        }
        //当处于末页的时候,'下一页'隐藏
        if (currentPage == (totalPage - 1)) {
            $nextPage.hide();
        }
        else {
            $nextPage.show();
        }
        $("#mypromotioncodeTotlaCount").html(totalCount)
        $("#mypromotioncodeTotlaPage").html(totalPage)
    }

    //添加返回的数据到tab
    function addResponseData(data)
    {
        if (globalStatus == 0) {
            $("#mypromotioncodelistTab1").empty();
            $('#mypromotioncodelistTab1').html(data);//替换成新的数据
        }
        else if (globalStatus == 1) {
            $("#mypromotioncodelistTab2").empty();
            $('#mypromotioncodelistTab2').html(data);//替换成新的数据
        }
        else if (globalStatus == 2) {
            $("#mypromotioncodelistTab3").empty();
            $("#mypromotioncodelistTab3").html(data);//替换成新的数据
        }
    }
    //首页
    $fristPage.click(function (e) {
        $.get("MyPromotionCodeByAjaxRequest?status=" + globalStatus + "&pageIndex=0", function (data) {
            currentPage = 0;
            addResponseData(data);
            contrlPageNumberDisplay();
        });
    });

    //上一页
    $previousPage.click(function (e) {
        $.get("MyPromotionCodeByAjaxRequest?status=" + globalStatus + "&pageIndex=" + (currentPage - 1), function (data) {
            currentPage = currentPage - 1;
            addResponseData(data);
            contrlPageNumberDisplay();
        });
    });

    //下一页
    $nextPage.click(function (e) {
        $.get("MyPromotionCodeByAjaxRequest?status=" + globalStatus + "&pageIndex=" + (currentPage + 1), function (data) {
            currentPage = currentPage + 1;
            addResponseData(data);
            contrlPageNumberDisplay();
        });
    });

    //末页
    $lastPage.click(function (e) {
        $.get("MyPromotionCodeByAjaxRequest?status=" + globalStatus + "&pageIndex=" + (totalPage - 1), function (data) {
            currentPage = totalPage - 1;
            addResponseData(data);
            contrlPageNumberDisplay();
        });
    });
});